package com.yingpai.fitness.dvp_2_0.main.fragment.shop;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.model.Response;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.fragment.CommonFragment;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.activity.shop.assmble.AssmbleShopDetailActivity;
import com.yingpai.fitness.dvp_2_0.main.adapter.shop.Dvp_2_1_ShopSaleRecyclerAdapter;
import com.yingpai.fitness.dvp_2_0.main.bean.assmble.AssmbleShopListBean;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.dvp_2_0.main.widget.CustomGridLayoutManage;
import com.yingpai.fitness.dvp_2_0.main.widget.ParentRecyclerView;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class AssmbleShopSaleFragment extends CommonFragment implements BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    private int categoryId;
    private int pageNum = 1;
    private int pageSize = 10;
    private Dvp_2_1_ShopSaleRecyclerAdapter mAdapter;
    Unbinder unbinder;
    @BindView(R.id.shop_sale_fragment_rv_list)
    ParentRecyclerView shop_sale_fragment_rv_list;
    @BindView(R.id.swiperefresh)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.empty)
    LinearLayout empty;
    @BindView(R.id.title_bar)
    RelativeLayout title_bar;

    /**
     * 推荐写法
     *
     * @param
     * @return
     */
    public static AssmbleShopSaleFragment getInstance(int categoryId) {
        AssmbleShopSaleFragment mShopSaleFragment = new AssmbleShopSaleFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("categoryId", categoryId);

        mShopSaleFragment.setArguments(bundle);
        return mShopSaleFragment;
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.shop_sale_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
        title_bar.setVisibility(View.GONE);
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setColorSchemeResources(R.color.common_color);

        categoryId = getArguments().getInt("categoryId", -1);

        /**
         * 设置布局管理器
         */
        shop_sale_fragment_rv_list.setLayoutManager(new CustomGridLayoutManage(context, 2));
        mAdapter = new Dvp_2_1_ShopSaleRecyclerAdapter(R.layout.dvp_2_1_shop_sale_list_item);
        mAdapter.setOnLoadMoreListener(this, shop_sale_fragment_rv_list);
        mAdapter.setOnItemChildClickListener(this);
        shop_sale_fragment_rv_list.setAdapter(mAdapter);

        return view;
    }

    @Override
    protected void initData() {
        showDialog(context, null);
        HashMap<String,Object> httpParams = new HashMap();
        httpParams.put("categoryId", categoryId);
        httpParams.put("pageNum", pageNum++);
        httpParams.put("pageSize", pageSize);
        finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.ASSMBLE_SHOP_LIST, httpParams, Url.ASSMBLE_SHOP_LIST, new Callback<AssmbleShopListBean>() {
            @Override
            public void onSuccess(AssmbleShopListBean assmbleShopListBean) {
                if (Constant.REQUEST_SUCCESS_0.equals(assmbleShopListBean.getCode())) {
                    if (pageNum - 1 == 1) {
                        if(assmbleShopListBean.getData().getPageInfo().getTotal()>0){
                            empty.setVisibility(View.GONE);
                            mAdapter.setNewData(assmbleShopListBean.getData().getPageInfo().getList());
                        }else{
                            empty.setVisibility(View.VISIBLE);
                        }
                    } else {
                        mAdapter.addData(assmbleShopListBean.getData().getPageInfo().getList());
                    }
                    swipeRefreshLayout.setRefreshing(false);
                    mAdapter.notifyDataSetChanged();

                    int pagecount;
                    if (assmbleShopListBean.getData().getPageInfo().getTotal() % pageSize == 0) {
                        pagecount = assmbleShopListBean.getData().getPageInfo().getTotal() / pageSize;
                    } else {
                        pagecount = assmbleShopListBean.getData().getPageInfo().getTotal() / pageSize + 1;
                    }

                    if (pageNum > pagecount) {
                        mAdapter.loadMoreEnd();
                    } else {
                        mAdapter.loadMoreComplete();
                        mAdapter.setEnableLoadMore(true);
                    }
                } else {
                    if (pageNum - 1 == 1) {
                        empty.setVisibility(View.VISIBLE);
                    } else {
                        empty.setVisibility(View.GONE);
                    }
                    showToast(assmbleShopListBean.getMessage()+"");
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissDialog();
                try{
                    if (pageNum - 1 == 1) {
                        empty.setVisibility(View.VISIBLE);
                    } else {
                        empty.setVisibility(View.GONE);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

//    @Override
//    @OnClick({R.id.shop_cart_iv})
//    public void onViewClicked(View view) {
//        super.onViewClicked(view);
//        switch (view.getId()) {
//            case R.id.shop_cart_iv:
//                startActivity(CartActivity.class);
//                break;
//        }
//    }


    /**
     * 适配器监听
     *
     * @param adapter
     * @param view
     * @param position
     */
    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.listitem_zone:
                Bundle bundle = new Bundle();
                bundle.putString("id", mAdapter.getItem(position).getId());
                startActivity(AssmbleShopDetailActivity.class, bundle);
                break;

        }
    }

    @Override
    public void onRefresh() {
        pageNum = 1;
        initData();
    }

    @Override
    public void onLoadMoreRequested() {
        initData();
    }

}
